﻿<UserControl x:Class="IBTrader.Frontend.HistoricalData.Control" Name="HistoricalData"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"
             xmlns:ctrlConn="clr-namespace:IBTrader.Frontend.Connection"
             xmlns:ctrlLog="clr-namespace:IBTrader.Frontend.Log"
             xmlns:con="http://schemas.kent.boogaart.com/converters"
             xmlns:sys="clr-namespace:System;assembly=mscorlib"
             xmlns:my="clr-namespace:IBTrader.Frontend.HistoricalData">
    <UserControl.DataContext>
        <my:ViewModel/>
    </UserControl.DataContext>
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\ResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>            
            <XmlDataProvider x:Key="BarSizes" XPath="BarSizes/BarSize">
                <x:XData>
                    <BarSizes xmlns="">
                        <BarSize>1 secs</BarSize>
                        <BarSize>5 secs</BarSize>
                        <BarSize>15 secs</BarSize>
                        <BarSize>30 secs</BarSize>
                        <BarSize>1 min</BarSize>
                        <BarSize>2 mins</BarSize>
                        <BarSize>3 mins</BarSize>
                        <BarSize>5 mins</BarSize>
                        <BarSize>15 mins</BarSize>
                        <BarSize>30 mins</BarSize>
                        <BarSize>1 hour</BarSize>
                        <BarSize>1 day</BarSize>
                    </BarSizes>
                </x:XData>
            </XmlDataProvider>
            <XmlDataProvider x:Key="BarTypes" XPath="BarTypes/BarType">
                <x:XData>
                    <BarTypes xmlns="">
                        <BarType>TRADES</BarType>
                        <BarType>MIDPOINT</BarType>
                        <BarType>BID</BarType>
                        <BarType>ASK</BarType>
                        <BarType>BID_ASK</BarType>
                        <BarType>HISTORICAL_VOLATILITY</BarType>
                        <BarType>OPTION_IMPLIED_VOLATILITY</BarType>
                    </BarTypes>
                </x:XData>
            </XmlDataProvider>
        </ResourceDictionary>
    </UserControl.Resources>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding LoadedCommand}" 
                                   CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}"/>
        </i:EventTrigger>
        <i:EventTrigger EventName="Closing" SourceObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">
            <i:InvokeCommandAction Command="{Binding UnloadedCommand}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <DockPanel LastChildFill="True">
        <Menu DockPanel.Dock="Top" Height="22">
            <ctrlConn:Menu />
            <ctrlLog:Menu />
            <MenuItem Header="Download">
                <MenuItem Header="Start" Command="{Binding StartDownload}"/>
                <MenuItem Header="Cancel" Command="{Binding CancelDownload}"/>
            </MenuItem>
        </Menu>
        <ctrlLog:Errors DockPanel.Dock="Top" />
        <ctrlLog:Status DockPanel.Dock="Bottom" Height="22" />
        <ScrollViewer>
            <StackPanel>
                <WrapPanel>
                    <Button Content="Start / Restart" Command="{Binding Start}" Margin="5" Padding="5"/>
                </WrapPanel>
                <Expander Header="Details">
                    <WrapPanel Orientation="Horizontal">
                        <ctrlConn:Control />
                        <GroupBox Header="Contract" >
                            <Grid VerticalAlignment="Top">
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="90" />
                                </Grid.ColumnDefinitions>
                                <Label Content="Symbol:" Grid.Row="0"/>
                                <Label Content="SecurityType:" Grid.Row="1" />
                                <Label Content="Currency:" Grid.Row="2"/>
                                <TextBox Grid.Row="0" Grid.Column="1" Name="Symbol" 
                                         Text="{Binding ElementName=HistoricalContracts, Path=SelectedItem.Symbol, UpdateSourceTrigger=PropertyChanged}"/>
                                <ComboBox Grid.Row="1" Grid.Column="1" Name="SecurityType" 
                                          ItemsSource="{Binding Source={StaticResource SecurityTypes}}"
                                          SelectedValue="{Binding ElementName=HistoricalContracts, Path=SelectedItem.SecurityType, UpdateSourceTrigger=PropertyChanged}"/>
                                <TextBox Grid.Row="2" Grid.Column="1" Name="Currency" 
                                         Text="{Binding ElementName=HistoricalContracts, Path=SelectedItem.Currency, UpdateSourceTrigger=PropertyChanged}"/>
                            </Grid>
                        </GroupBox>
                        <GroupBox Header="Contract Extended" >
                            <Grid VerticalAlignment="Top">
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="90" />
                                </Grid.ColumnDefinitions>
                                <Label Content="Exchange:" Grid.Row="0"/>
                                <Label Content="Primary Exchange:" Grid.Row="1" />
                                <Label Content="Multiplier:" Grid.Row="2" />
                                <ComboBox Grid.Row="0" Grid.Column="1" Name="Exchange" 
                                          ItemsSource="{Binding Source={StaticResource Exchanges}}"
                                          SelectedValue="{Binding ElementName=HistoricalContracts, Path=SelectedItem.Exchange, UpdateSourceTrigger=PropertyChanged}" />
                                <TextBox Grid.Row="1" Grid.Column="1" Name="PrimaryExchange" 
                                         Text="{Binding ElementName=HistoricalContracts, Path=SelectedItem.PrimaryExchange, UpdateSourceTrigger=PropertyChanged}"/>
                                <TextBox Grid.Row="2" Grid.Column="1" Name="Multiplier" 
                                         Text="{Binding ElementName=HistoricalContracts, Path=SelectedItem.Multiplier, UpdateSourceTrigger=PropertyChanged}"/>
                            </Grid>
                        </GroupBox>
                        <GroupBox Header="Bars">
                            <Grid VerticalAlignment="Top">
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="100" />
                                </Grid.ColumnDefinitions>
                                <Label Content="Size:" Grid.Row="0" />
                                <Label Content="Duration:" Grid.Row="1"/>
                                <Label Content="Type:" Grid.Row="2" />
                                <ComboBox Grid.Row="0" Grid.Column="1" Name="BarSize"
                                          ItemsSource="{Binding Source={StaticResource BarSizes}}"
                                          SelectedValue="{Binding ElementName=HistoricalContracts, Path=SelectedItem.BarSize, UpdateSourceTrigger=PropertyChanged}" />
                                <TextBox Grid.Row="1" Grid.Column="1" Name="BarDuration" IsReadOnly="True" 
                                         Text="{Binding ElementName=HistoricalContracts, Path=SelectedItem.BarDuration, UpdateSourceTrigger=PropertyChanged}"/>
                                <ComboBox Grid.Row="2" Grid.Column="1" Name="BarType" 
                                          ItemsSource="{Binding Source={StaticResource BarTypes}}"
                                          SelectedValue="{Binding ElementName=HistoricalContracts, Path=SelectedItem.BarType, UpdateSourceTrigger=PropertyChanged}" />
                            </Grid>
                        </GroupBox>
                        <GroupBox Header="Range">
                            <Grid VerticalAlignment="Top">
                                <Grid.RowDefinitions>
                                    <RowDefinition />
                                    <RowDefinition />
                                    <RowDefinition />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition Width="100" />
                                </Grid.ColumnDefinitions>
                                <Label Content="From:" Grid.Row="0"/>
                                <Label Content="To:" Grid.Row="1" />
                                <Label Content="OnlyRealTime:" Grid.Row="2" />
                                <DatePicker Grid.Row="0" Grid.Column="1" Name="From" 
                                            SelectedDate="{Binding ElementName=HistoricalContracts, Path=SelectedItem.From, UpdateSourceTrigger=PropertyChanged}"/>
                                <DatePicker Grid.Row="1" Grid.Column="1" Name="To" 
                                            SelectedDate="{Binding ElementName=HistoricalContracts, Path=SelectedItem.To, UpdateSourceTrigger=PropertyChanged}"/>
                                <CheckBox Grid.Row="2" Grid.Column="1" Name="OnlyRealTime" 
                                          IsChecked="{Binding ElementName=HistoricalContracts, Path=SelectedItem.OnlyRealTime, UpdateSourceTrigger=PropertyChanged}" />
                            </Grid>
                        </GroupBox>
                    </WrapPanel>
                </Expander>
                <Expander Header="Configurations" IsExpanded="True">
                    <DataGrid Name="HistoricalContracts" AutoGenerateColumns="False" 
                              ItemsSource="{Binding Path=Store.HistoricalContracts, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="InitializingNewItem">
                                <cmd:EventToCommand Command="{Binding DataGridInitializingNewItem}" PassEventArgsToCommand="True"/>
                            </i:EventTrigger>
                            <i:EventTrigger EventName="CurrentCellChanged">
                                <i:InvokeCommandAction 
                                    CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}} }"
                                    Command="{Binding DataGridCurrentCellChanged}" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Symbol" Binding="{Binding Symbol, UpdateSourceTrigger=PropertyChanged}" />
                            <DataGridComboBoxColumn Header="SecurityType" 
                                                    ItemsSource="{Binding Source={StaticResource SecurityTypes}}"
                                                    SelectedValueBinding="{Binding SecurityType, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="Currency" Binding="{Binding Currency, UpdateSourceTrigger=PropertyChanged}" />
                            <DataGridComboBoxColumn Header="Exchange" IsReadOnly="False"
                                                    ItemsSource="{Binding Source={StaticResource Exchanges}}"
                                                    SelectedValueBinding="{Binding Exchange, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="PrimaryExchange" Binding="{Binding PrimaryExchange, UpdateSourceTrigger=PropertyChanged}" />
                            <DataGridTextColumn Header="Multiplier" Binding="{Binding Multiplier, UpdateSourceTrigger=PropertyChanged}" />
                            <DataGridComboBoxColumn Header="BarSize" IsReadOnly="False"
                                                    ItemsSource="{Binding Source={StaticResource BarSizes}}"
                                                    SelectedValueBinding="{Binding BarSize, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="BarDuration" Binding="{Binding BarDuration, UpdateSourceTrigger=PropertyChanged}" />
                            <DataGridComboBoxColumn Header="BarType" IsReadOnly="False"
                                                    ItemsSource="{Binding Source={StaticResource BarTypes}}"
                                                    SelectedValueBinding="{Binding BarType, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTemplateColumn>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker Name="From" SelectedDate="{Binding Path=From, UpdateSourceTrigger=PropertyChanged}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <DatePicker Name="To" SelectedDate="{Binding Path=To, UpdateSourceTrigger=PropertyChanged}"/>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridCheckBoxColumn Header="OnlyRealTime" Binding="{Binding OnlyRealTime, UpdateSourceTrigger=PropertyChanged}"/>
                            <DataGridTextColumn Header="Percent" IsReadOnly="True" Binding="{Binding Percent}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                </Expander>
                <Expander Header="Log" IsExpanded="True">
                    <ctrlLog:Control />
                </Expander>
            </StackPanel>
        </ScrollViewer>
    </DockPanel>
</UserControl>
